package com.eschool.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.DualListModel;
import org.primefaces.model.UploadedFile;

import com.eschool.beans.converter.CourseConverter;
import com.eschool.dao.CourseDao;
import com.eschool.model.Course;
import com.eschool.model.Teacher;
import com.eschool.service.TeacherService;
import com.eschool.service.impl.TeacherServiceImpl;

@ManagedBean(name="teacherBean")
@ViewScoped
public class TeacherBean {

	private Teacher teacher = new Teacher();
	TeacherService teacherService = new TeacherServiceImpl();
	UploadedFile cv;

	public UploadedFile getCv() {
		return cv;
	}

	public void setCv(UploadedFile cv) {
		this.cv = cv;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	 /*
	public void handleFileUpload(FileUploadEvent event) {
			FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
	 */
		
	private DualListModel<Course> courses;

	public DualListModel<Course> getCourses() {
		return courses;
	}

	public void setCourses(DualListModel<Course> courses) {
		this.courses = courses;
	}
/*
	private DualListModel<String> names;
	private List<String> nam1;
	private List<String> nam2;

	public List<String> getNam1() {
		return nam1;
	}

	public void setNam1(List<String> nam1) {
		this.nam1 = nam1;
	}

	public List<String> getNam2() {
		return nam2;
	}

	public void setNam2(List<String> nam2) {
		this.nam2 = nam2;
	}*/

	public TeacherBean() {
		courses = new DualListModel<Course>(CourseConverter.courseDB,Collections.EMPTY_LIST);
	}
/*
	public DualListModel<String> getNames() {
		return names;
	}
	public void setNames(DualListModel<String> names) {
		this.names = names;
	}*/
	
	public String perform()
	{
		String message;
		teacher.setCourses(new HashSet<Course>(courses.getTarget()));
		teacher.setCv("no cv");

		if(teacherService.addTeacher(teacher)) {
			message = "Teacher Successfully Added";
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info message", message));
		}
		else {
			message = "Teacher Not Added";
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error message", message));
		}
		
		return "AddTeacher";
}
}
